﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace PIM_ATE.rflight
{
    class ClsRfPIMforDesktop_ControlSoftware : JcPIMRunner
    {
        const string lf = "";
        //初始化状态
        bool m_isInited;
        //设备列表(NAME, IP)
        Dictionary<string, JcPIMInstrumentInfo> m_devices;
        //连接实例
        ClsRfPimCmd_ControlSoftware m_compatObj;
        //当前设置
        JcPIMDataSettings m_pim_settings;
        //结果列表
        List<JcPIMDataResults> m_pim_resultLists = new List<JcPIMDataResults>();
        //控制实例
        ImpCtrl m_ctrl;
        //异步句柄
        delegate bool DelStartTest();

        public ClsRfPIMforDesktop_ControlSoftware(Dictionary<string, JcPIMInstrumentInfo> devices)
        {
            m_devices = devices;
            m_compatObj = new ClsRfPimCmd_ControlSoftware(GDefine.IS_COMPAT_DEBUG_TEST);
            m_isInited = false;
            m_ctrl = new ImpCtrl();
        }
        public override void start_settings(JcPIMDataSettings settings)
        {
            m_pim_settings = settings;
            m_pim_resultLists.Clear();
            if (settings.isRandom || GDefine.IS_COMPAT_DEBUG_TEST)
            {
                m_compatObj.SetDebug(true);
                m_compatObj.SetDebugMaxMin(settings.RandomMax, settings.RandomMin);
            }
            else
            {
                m_compatObj.SetDebug(false);
            }
        }
        public override void start_test()
        {
            DelStartTest del_start_test = new DelStartTest(start_test_sync);
            del_start_test.BeginInvoke(evt_complete_test_async, del_start_test);
        }
        public override void stop_test()
        {
            Monitor.Enter(m_ctrl);
            m_ctrl.bQuit = true;
            Monitor.Exit(m_ctrl);
        }
        bool start_test_sync()
        {
            bool complete = false;
            if (init(m_devices[m_pim_settings.Band_Name].instrAddress))
            {
                if (m_pim_settings.Mode == ImMode.Freq)
                    complete = freq_sweep_Auto();
                else
                    complete = time_sweep();
            }
            return complete;
        }
        void evt_show_msg(string msg, MSG_TYPE type)
        {
            //if (type != MSG_TYPE.LOG_NET)
            On_evt_show_log_handle(msg, null);

            if (type == MSG_TYPE.ERROR_NET || type == MSG_TYPE.ERROR_SET || type == MSG_TYPE.ERROR_TEST)
            {
                On_evt_show_error_handle(msg, null);
                //evt_complete_test(null, null);
            }
        }
        void evt_update_ui(object obj, EventArgs e)
        {//完成点事件
            On_evt_get_point_handle(obj, e);
        }
        void evt_complete_test_async(IAsyncResult result)
        {//完成测试事件(异步)
            bool complete = (result.AsyncState as DelStartTest).EndInvoke(result);

            if (m_isInited) m_compatObj.Close();
            m_isInited = false;
            evt_show_msg("connect: closen" + lf, MSG_TYPE.LOG_NET);

            if (complete)
                On_evt_complete_test_handle(m_pim_resultLists, null);
            else
                On_evt_complete_test_handle(null, null);
        }
        bool init(string address)
        {//初始化
            if (m_compatObj.Connect(address))
            {//连接
                evt_show_msg("connect: success" + lf, MSG_TYPE.LOG_NET);
                m_isInited = true;
            }
            else
            {
                m_isInited = false;
                evt_show_msg("connect-Error: " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
            }

            return m_isInited;
        }

        bool freq_sweep()
        {//扫频
            Monitor.Enter(m_ctrl);
            m_ctrl.bQuit = false;
            Monitor.Exit(m_ctrl);

            double freq_mhz = 0;
            double val = 0;
            double p1 = 0, p2 = 0;
            int sweeptype = 0;

            if (!m_compatObj.setFreqBand(0))
            {//1.选择第一个系统频段："SYST:FREQ:BAND 0\n"
                evt_show_msg("exec-Error(setFreqBand): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setMeasMode(false))
            {//2.设置系统测量模式为反射："SYST:MEAS:MODE REFL\n"
                evt_show_msg("exec-Error(setMeasMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setFreqMode(true))
            {//3.设置点频模式："FREQ:MODE FIX\n"
                evt_show_msg("exec-Error(setFreqMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }

            if (!m_compatObj.setPimOrd((int)m_pim_settings.Order))
            {//5.设置系统互调阶数位3："SYST:PIM:ORD 3\n"
                evt_show_msg("exec-Error(setPimOrd): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setCarrPow(m_pim_settings.TX1 + m_pim_settings.Offset1))
            {//6.设置载波功率为43.00 dBm："CARR:POW 43.00\n"
                evt_show_msg("exec-Error(setCarrPow): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setInitCont(false))
            {//8.设置连续启动模式开启："INIT:CONT ON\n"
                evt_show_msg("exec-Error(setInitCont): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }

            ////ClsJcPimCompat.CHANNEL channel;
            ////if (m_pim_settings.Channel == 0)
            ////    channel = ClsJcPimCompat.CHANNEL.REV;
            ////else
            ////    channel = ClsJcPimCompat.CHANNEL.FWD;

            //if (m_pim_settings.Channel == 0)
            //{
            //    if (!m_compatObj.fnSetChannel(ClsJcPimCompat.CHANNEL.REV, 1))
            //    {//设置端口
            //        evt_show_msg("exec-Error(fnSetChannel): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
            //        return false;
            //    }
            //}

            ////if (!m_compatObj.fnSetTxOn(true))
            ////{//开启功放
            ////    evt_show_msg("exec-Error(fnSetTxOn): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
            ////    return false;
            ////}

            List<XPlot> lstPoint = new List<XPlot>();
            //扫频状态
            List<int> SweepType = new List<int>();
            List<int> IsUpSweep = new List<int>();
            List<int> IsDownSweep = new List<int>();
            double n1 = 0, n2 = 0, f = 0, g = 0;
            if (m_pim_settings.StepRx == 0)
            {//TX步进
                if (m_pim_settings.Step1 <= 0) m_pim_settings.Step1 = 1;
                n1 = Math.Ceiling((m_pim_settings.F1e - m_pim_settings.F1s) / m_pim_settings.Step1) + 1;
                if (m_pim_settings.Step2 <= 0) m_pim_settings.Step2 = 1;
                n2 = Math.Ceiling((m_pim_settings.F2e - m_pim_settings.F2s) / m_pim_settings.Step2) + 1;

                f = m_pim_settings.F1s;
                for (int i = 0; i < n1; ++i)
                {//上扫频
                    if (f > m_pim_settings.F1e) f = m_pim_settings.F1e;
                    double fpim = sumPIMFreq(f, m_pim_settings.F2e, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    lstPoint.Add(new XPlot(f, m_pim_settings.F2e, fpim));
                    f += m_pim_settings.Step1;
                    if (IsUpSweep.Count == 0)
                    {
                        IsUpSweep.Add(0);
                    }
                    else
                    {
                        IsUpSweep.Add(1);
                    }

                }

                f = m_pim_settings.F2e;
                for (int i = 0; i < n2; ++i)
                {//下扫频
                    if (f < m_pim_settings.F2s) f = m_pim_settings.F2s;
                    double fpim = sumPIMFreq(m_pim_settings.F1s, f, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    lstPoint.Add(new XPlot(m_pim_settings.F1s, f, fpim));
                    f -= m_pim_settings.Step2;
                    if (IsDownSweep.Count == 0)
                    {
                        IsDownSweep.Add(0);
                    }
                    else
                    {
                        IsDownSweep.Add(2);
                    }
                }
            }
            else
            {//rx步进
                double n = Math.Ceiling((m_pim_settings.RXe - m_pim_settings.RXs) / m_pim_settings.StepRx) + 1;
                f = m_pim_settings.RXs;
                for (int i = 0; i < n; ++i, f += m_pim_settings.StepRx)
                {
                    if (f > m_pim_settings.RXe) f = m_pim_settings.RXe;

                    double f1 = sumF1FromPIMFreq(m_pim_settings.F2Set, f, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    if (f1 >= m_pim_settings.F1Set && f1 <= m_pim_settings.F2Set)
                    {
                        g = sumPIMFreq(f1, m_pim_settings.F2Set, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                        lstPoint.Add(new XPlot(f1, m_pim_settings.F2Set, g));
                        n1++;
                        if (IsUpSweep.Count == 0)
                        {
                            IsUpSweep.Add(0);
                        }
                        else
                        {
                            IsUpSweep.Add(1);
                        }
                    }

                    f1 = sumF2FromPIMFreq(m_pim_settings.F2Set, f, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    if (f1 >= m_pim_settings.F1Set && f1 <= m_pim_settings.F2Set)
                    {
                        g = sumPIMFreq(f1, m_pim_settings.F2Set, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                        lstPoint.Add(new XPlot(f1, m_pim_settings.F2Set, g));
                        n1++;
                        if (IsUpSweep.Count == 0)
                        {
                            IsUpSweep.Add(0);
                        }
                        else
                        {
                            IsUpSweep.Add(1);
                        }
                    }
                }

                f = m_pim_settings.RXs;
                for (int i = 0; i < n; ++i, f += m_pim_settings.StepRx)
                {
                    if (f > m_pim_settings.RXe) f = m_pim_settings.RXe;
                    double f2 = sumF2FromPIMFreq(m_pim_settings.F1Set, f, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    if (f2 >= m_pim_settings.F1Set && f2 <= m_pim_settings.F2Set)
                    {
                        g = sumPIMFreq(m_pim_settings.F1Set, f2, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                        lstPoint.Add(new XPlot(m_pim_settings.F1Set, f2, g));
                        n2++;
                        if (IsDownSweep.Count == 0)
                        {
                            IsDownSweep.Add(0);
                        }
                        else
                        {
                            IsDownSweep.Add(2);
                        }
                    }

                    f2 = sumF1FromPIMFreq(m_pim_settings.F1Set, f, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                    if (f2 >= m_pim_settings.F1Set && f2 <= m_pim_settings.F2Set)
                    {
                        g = sumPIMFreq(m_pim_settings.F1Set, f2, (int)m_pim_settings.Order, m_pim_settings.isHigh);
                        lstPoint.Add(new XPlot(m_pim_settings.F1Set, f2, g));
                        n2++;
                        if (IsDownSweep.Count == 0)
                        {
                            IsDownSweep.Add(0);
                        }
                        else
                        {
                            IsDownSweep.Add(2);
                        }
                    }
                }
            }

            DateTime time = DateTime.Now;
            for (int j = 0; j < IsDownSweep.Count; j++)
            {
                IsUpSweep.Add(IsDownSweep[j]);
            }
            SweepType = IsUpSweep;
            int sweep = 0;
            #region 原循环测试
            for (int i = 0; i < lstPoint.Count; ++i)
            {//循环测试            
                Monitor.Enter(m_ctrl);
                bool isQuit = m_ctrl.bQuit;
                Monitor.Exit(m_ctrl);
                if (isQuit)
                {//手动结束
                    m_compatObj.setInitImm(false);
                    evt_show_msg("exec: test stop" + lf, MSG_TYPE.LOG_TEST);
                    return true;
                }
                if (!m_compatObj.setCarrFreq(lstPoint[i].X1, lstPoint[i].X2, "mhz"))
                {//4.设置载波1频率为1805.0 MHz，载波2频率为1880.0 MHz："CARR:FREQ 1805.0,1880.0\n"
                    evt_show_msg("exec-Error(setCarrFreq): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }

                if (!m_compatObj.setInitImm())
                {//9.设置立即启动测试："INIT:IMM\n"
                    evt_show_msg("exec-Error(setInitImm): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }


                /******************************************/
                if (!m_compatObj.getErr())
                {//查询系统错误
                    evt_show_msg("exec-Error(getErr): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }
                /******************************************/



                evt_show_msg("exec: Set f1 = " + lstPoint[i].X1.ToString() + " ,f2 = " +
                                                 lstPoint[i].X2.ToString() + lf, MSG_TYPE.LOG_TEST);
                if (!m_compatObj.getCarrPow(ref p1, ref p2))
                {//获取功率
                    p1 = p2 = m_pim_settings.TX1 + m_pim_settings.Offset1;
                }
                if (!m_compatObj.getOpc())
                {//10.连续查询测试完成状态："*OPC?\n"，直至返回状态为1。
                    evt_show_msg("exec-Error(getOpc): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }
                freq_mhz = lstPoint[i].RX;
                if (!m_compatObj.getFixPimData(freq_mhz, ref val))
                {//11.查询点频互调数据："FIX:PIM:DATA?\n"
                    m_compatObj.setInitImm(false);
                    evt_show_msg("exec-Error(getFixPimData): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }
                val += m_pim_settings.Offset_RX;

                JcPIMDataResults entry = new JcPIMDataResults();
                entry.No = i + 1;
                entry.F1 = (float)lstPoint[i].X1;
                entry.F2 = (float)lstPoint[i].X2;
                entry.Im_F = (float)freq_mhz;
                entry.Im_V = (float)val;
                entry.P1 = (float)Math.Round(p1, 1);
                entry.P2 = (float)Math.Round(p2, 1);
                entry.n1 = (int)n1;
                entry.n2 = (int)n2;
                if (m_pim_settings.isRandom)
                {
                    entry.Time_now = time;
                    time = time.AddSeconds(0.5);
                }
                else
                {
                    entry.Time_now = DateTime.Now;
                }

                m_pim_resultLists.Add(entry);
                evt_update_ui(entry, null);
            }
            #endregion
            m_compatObj.setInitImm(false);
            return true;
        }

        bool freq_sweep_Auto()
        {
            Monitor.Enter(m_ctrl);
            m_ctrl.bQuit = false;
            Monitor.Exit(m_ctrl);

            double val = 0, p1 = 0, p2 = 0, n1 = 0, n2 = 0;

            if (!m_compatObj.setFreqBand(0))
            {//1.选择第一个系统频段："SYST:FREQ:BAND 0\n"
                evt_show_msg("exec-Error(setFreqBand): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setMeasMode(false))
            {//2.设置系统测量模式为反射："SYST:MEAS:MODE REFL\n"
                evt_show_msg("exec-Error(setMeasMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setFreqMode(false))
            {//3.设置点频模式："FREQ:MODE FIX\n"
                evt_show_msg("exec-Error(setFreqMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }


            /****************************TXL-TXH****************************/
            if (!m_compatObj.setCarrFreq(m_pim_settings.F1Set, m_pim_settings.F2Set))
            {//TXL-TXH--->4.扫频设置起始终止频段范围  单位MHz  "CARR:FREQ 1805.0,1880.0\n"
                evt_show_msg("exec-Error(setCarrFreq): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            /****************************TXL-TXH****************************/
            /****************************RXL-RXH****************************/
            if (!m_compatObj.setIMFreqRang(m_pim_settings.RXs, m_pim_settings.RXe))
            {//RXL-RXH
                evt_show_msg("exec-Error(setIMFreqRang): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            /****************************RXL-RXH****************************/



            if (!m_compatObj.setPimOrd((int)m_pim_settings.Order))
            {//5.设置系统互调阶数位3："SYST:PIM:ORD 3\n"
                evt_show_msg("exec-Error(setPimOrd): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setSweStep(m_pim_settings.StepRx))
            {//6.设置扫频步进
                evt_show_msg("exec-Error(setPimOrd): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setCarrPow(m_pim_settings.TX1 + m_pim_settings.Offset1))
            {//7.设置载波功率为43.00 dBm："CARR:POW 43.00\n"
                evt_show_msg("exec-Error(setCarrPow): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setInitCont(false))
            {//8.设置连续启动模式开启："INIT:CONT ON\n"
                evt_show_msg("exec-Error(setInitCont): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setInitImm())
            {//设置立即启动测试："INIT:IMM\n"
                evt_show_msg("exec-Error(setInitImm): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.getErr())
            {//查询系统错误
                evt_show_msg("exec-Error(getErr): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            DateTime time = DateTime.Now;

            #region 轮循  获取PIM
            int count = 0; bool isUpTracEND = false; bool isDnTracEND = false;
            List<XPlot> lstXPlot = new List<XPlot>();
            try
            {
                bool isDone = false;
                do
                {
                    Monitor.Enter(m_ctrl);
                    bool isQuit = m_ctrl.bQuit;
                    Monitor.Exit(m_ctrl);
                    if (isQuit)
                    {//手动结束
                        m_compatObj.setInitImm(false);
                        evt_show_msg("exec: test stop" + lf, MSG_TYPE.LOG_TEST);
                        return true;
                    }
                    if (!m_compatObj.getCarrPow(ref p1, ref p2))
                    {//获取功率
                        p1 = p2 = m_pim_settings.TX1 + m_pim_settings.Offset1;
                    }
                    Thread.Sleep(50);
                    int nextP = lstXPlot.Count;
                    if (!isUpTracEND)
                    {
                        m_compatObj.getSwePoint(ref lstXPlot, out isUpTracEND, ref n1, ref n2);
                    }
                    if (isUpTracEND && !isDnTracEND)
                    {
                        m_compatObj.getSwePoint(ref lstXPlot, out isDnTracEND, ref n1, ref n2, false);
                    }
                    for (int i = count; i < lstXPlot.Count; i++)
                    {
                        evt_show_msg("exec: Set f1 = " + lstXPlot[i].X1.ToString() + " ,f2 = " +
                                                    lstXPlot[i].X2.ToString() + lf, MSG_TYPE.LOG_TEST);
                        val = lstXPlot[i].VAL + m_pim_settings.Offset_RX;

                        JcPIMDataResults entry = new JcPIMDataResults();
                        entry.No = count++;
                        entry.F1 = (float)lstXPlot[i].X1;
                        entry.F2 = (float)lstXPlot[i].X2;
                        entry.Im_F = (float)lstXPlot[i].RX;
                        entry.Im_V = (float)val;
                        entry.P1 = (float)Math.Round(p1, 1);
                        entry.P2 = (float)Math.Round(p2, 1);
                        entry.n1 = (int)n1;
                        entry.n2 = (int)n2;
                        if (m_pim_settings.isRandom)
                        {
                            entry.Time_now = time;
                            time = time.AddSeconds(0.5);
                        }
                        else
                        {
                            entry.Time_now = DateTime.Now;
                        }

                        m_pim_resultLists.Add(entry);
                        evt_update_ui(entry, null);
                    }
                    if (isDone)
                    {
                        break;
                    }
                    if (m_compatObj.getOpc_Time())
                    {
                        isDone = true;
                    }
                }
                while (true);
            }
            catch (Exception ex)
            {
                evt_show_msg(ex.ToString(), MSG_TYPE.LOG_TEST);
            }
            #endregion
            m_compatObj.setInitImm(false);
            return true;
        }
        bool time_sweep()
        {//点频
            Monitor.Enter(m_ctrl);
            m_ctrl.bQuit = false;
            Monitor.Exit(m_ctrl);

            double freq_mhz = 0;
            double val = 0;
            double p1 = 0, p2 = 0;

            if (!m_compatObj.setFreqBand(0))
            {//1.选择第一个系统频段："SYST:FREQ:BAND 0\n"
                evt_show_msg("exec-Error(setFreqBand): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setMeasMode(false))
            {//2.设置系统测量模式为反射："SYST:MEAS:MODE REFL\n"
                evt_show_msg("exec-Error(setMeasMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setFreqMode(true))
            {//3.设置点频模式："FREQ:MODE FIX\n"
                evt_show_msg("exec-Error(setFreqMode): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setCarrFreq(m_pim_settings.F1s, m_pim_settings.F2e, "mhz"))
            {//4.设置载波1频率为1805.0 MHz，载波2频率为1880.0 MHz："CARR:FREQ 1805.0,1880.0\n"
                evt_show_msg("exec-Error(setCarrFreq): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            /****************设置RX接收通带,点频将算出来的范围卡死*************************/
            /****************************RXL-RXH****************************/
            if (!m_compatObj.setIMFreqRang(m_pim_settings.RXs - 10, m_pim_settings.RXe + 10))
            {//RXL-RXH
                evt_show_msg("exec-Error(setIMFreqRang): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }

            /****************************RXL-RXH****************************/
            /****************设置RX接收通带,点频将算出来的范
             * 围卡死*************************/
            if (!m_compatObj.setPimOrd((int)m_pim_settings.Order))
            {//5.设置系统互调阶数位3："SYST:PIM:ORD 3\n"
                evt_show_msg("exec-Error(setPimOrd): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setCarrPow(m_pim_settings.TX1 + m_pim_settings.Offset1))
            {//6.设置载波功率为43.00 dBm："CARR:POW 43.00\n"
                evt_show_msg("exec-Error(setCarrPow): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setMeasTime(m_pim_settings.Time))
            {//7.设置系统测量时间为5秒："SYST:MEAS:TIME 5\n"
                evt_show_msg("exec-Error(setCarrPow): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            if (!m_compatObj.setInitCont(true))
            {//8.设置连续启动模式开启："INIT:CONT ON\n"
                evt_show_msg("exec-Error(setInitCont): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }

            if (!m_compatObj.setInitImm())
            {//9.设置立即启动测试："INIT:IMM\n"
                evt_show_msg("exec-Error(setInitImm): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }


            /***************查询系统错误***************************/
            if (!m_compatObj.getErr())
            {//查询系统错误
                evt_show_msg("exec-Error(getErr): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                return false;
            }
            /***************查询系统错误***************************/


            int i = 0;
            long time_start = DateTime.Now.Ticks;
            DateTime time = DateTime.Now;
            while (true)
            {
                Monitor.Enter(m_ctrl);
                bool isQuit = m_ctrl.bQuit;
                Monitor.Exit(m_ctrl);
                if (isQuit)
                {//手动结束测试
                    m_compatObj.setInitImm(false);
                    evt_show_msg("exec: test stop" + lf, MSG_TYPE.LOG_TEST);
                    return true;
                }

                TimeSpan elapsedSpan = new TimeSpan(DateTime.Now.Ticks - time_start);
                if (elapsedSpan.TotalSeconds >= m_pim_settings.Time)
                {
                    break;
                }
                if (m_compatObj.getOpc_Time())
                {//10.连续查询测试完成状态："*OPC?\n"，直至返回状态为1。                 
                    break;
                }
                if (!m_compatObj.getCarrPow(ref p1, ref p2))
                {//获取功率
                    p1 = p2 = m_pim_settings.TX1 + m_pim_settings.Offset1;
                }

                freq_mhz = m_pim_settings.RXs;
                if (!m_compatObj.getFixPimData(freq_mhz, ref val))
                {//11.查询点频互调数据："FIX:PIM:DATA?\n"
                    m_compatObj.setInitImm(false);
                    evt_show_msg("exec-Error(getFixPimData): " + m_compatObj.fnGetError() + lf, MSG_TYPE.ERROR_NET);
                    return false;
                }
                val += m_pim_settings.Offset_RX;

                JcPIMDataResults entry = new JcPIMDataResults();
                entry.No = i;
                entry.F1 = (float)m_pim_settings.F1s;
                entry.F2 = (float)m_pim_settings.F2e;
                entry.Im_F = (float)freq_mhz;
                entry.Im_V = (float)val;
                entry.P1 = (float)p1;
                entry.P2 = (float)p2;
                entry.n1 = 0;
                entry.n2 = 0;
                if (m_pim_settings.isRandom)
                {
                    entry.Time_now = time;
                    time = time.AddSeconds(0.5);
                }
                else
                {
                    entry.Time_now = DateTime.Now;
                }
                m_pim_resultLists.Add(entry);
                evt_update_ui(entry, null);
                i++;
            }
            m_compatObj.setInitImm(false);
            return true;
        }


    }
}
